package com.lwj.controller.test;

import com.lwj.annotation.RequirePermission;
import com.lwj.excel.DepartmentTreeExcel;
import com.lwj.mapper.administration.DepartmentMapper;
import com.lwj.mapper.administration.UserMapper;
import com.lwj.service.DozerService;
import com.lwj.service.HelloService;
import com.lwj.service.common.file.FileService;
import com.lwj.service.configuration.PermissionService;
import com.lwj.utils.excel.EasyExcelUtil;
import com.lwj.utils.excel.SheetInfo;
import com.lwj.vo.administration.DepartmentTreeVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "接口测试")
@RestController()
@RequestMapping("/api/hello")
public class HelloController {
    protected static final Logger logger = LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private HelloService helloService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FileService fileService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DozerService dozerService;
    @GetMapping("/get")
    public String test4()  {
        return "12312312";
    }
    @GetMapping("/test")
    public void test(HttpServletResponse response)  {
        helloService.text3(response);
    }

    @GetMapping("/textAsync")
    public String test1() throws Exception {
        for (int i = 0; i < 100; i++) {
            helloService.textAsync();
        }
        return "122";
    }
    @GetMapping("/test2")
    @RequirePermission("api.optionOne.select")
    public void test2(HttpServletResponse response) throws Exception {
        List<DepartmentTreeVo> departmentTreeVos = departmentMapper.listDepartmentTree("1000", false);
        List<DepartmentTreeExcel> excels = dozerService.mapList(departmentTreeVos, DepartmentTreeExcel.class);
        EasyExcelUtil easyExcelUtil = new EasyExcelUtil();
        SheetInfo<DepartmentTreeExcel> sheetInfo=new SheetInfo<>("sheet",DepartmentTreeExcel.class,excels,1,easyExcelUtil.getWriteHandlerList());
        SheetInfo<DepartmentTreeExcel> sheetInfo1=new SheetInfo<>("sheet1",DepartmentTreeExcel.class,excels,2,easyExcelUtil.getWriteHandlerList());
        SheetInfo<DepartmentTreeExcel> sheetInfo2=new SheetInfo<>("sheet2",DepartmentTreeExcel.class,excels,3,easyExcelUtil.getWriteHandlerList());

        List<SheetInfo> list=new ArrayList<>();
        list.add(sheetInfo);
        list.add(sheetInfo1);
        list.add(sheetInfo2);
        easyExcelUtil.writeSheets(response,"test",list);
    }

    protected final HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    protected final HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

}
